﻿<template>
	<div class="iotdevice-container">
		<el-dialog v-model="isShowDialog" :width="800" draggable="" :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<!--<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>-->
					<span>{{ props.title }}</span>
				</div>
			</template>
			<el-form :model="ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.设备名称')" prop="deviceName">
							<el-input v-model="ruleForm.deviceName" :placeholder="concatI18nT('message.common.请输入','message.iotdevice.设备名称')" maxlength="64" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.产品ID')" prop="productId">
							<el-select clearable v-model="ruleForm.productId" placeholder="请选择产品ID">
								<el-option v-for="(item,index) in productsData"  :key="index" :value="item.id" :label="item.productName" @click.native="changeProduct(item.productName)"></el-option>
								
							</el-select>
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.产品名称')" prop="productName">
							<el-input v-model="ruleForm.productName" :placeholder="concatI18nT('message.common.请输入','message.iotdevice.产品名称')" maxlength="64" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.设备编号')" prop="serialNumber">
							<el-input v-model="ruleForm.serialNumber" :placeholder="concatI18nT('message.common.请输入','message.iotdevice.设备编号')" maxlength="64" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.子设备网关编号')" prop="gwDevCode">
							<el-input v-model="ruleForm.gwDevCode" :placeholder="concatI18nT('message.common.请输入','message.iotdevice.子设备网关编号')" maxlength="64" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.固件版本')" prop="firmwareVersion">
							<el-input v-model="ruleForm.firmwareVersion" :placeholder="concatI18nT('message.common.请输入','message.iotdevice.固件版本')" maxlength="11" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.设备状态')" prop="status">
							<el-select clearable v-model="ruleForm.status" placeholder="请选择设备状态">
								<el-option v-for="(item,index) in dl('iot_device_status')"  :key="index" :value="Number(item.code)" :label="`[${item.code}] ${item.value}`"></el-option>
								
							</el-select>
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.物模型值')" prop="thingsModelValue">
							<el-input v-model="ruleForm.thingsModelValue" :placeholder="concatI18nT('message.common.请输入','message.iotdevice.物模型值')" maxlength="0" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.设备所在地址')" prop="networkAddress">
							<el-input v-model="ruleForm.networkAddress" :placeholder="concatI18nT('message.common.请输入','message.iotdevice.设备所在地址')" maxlength="255" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.设备入网IP')" prop="networkIp">
							<el-input v-model="ruleForm.networkIp" :placeholder="concatI18nT('message.common.请输入','message.iotdevice.设备入网IP')" maxlength="32" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.设备经度')" prop="longitude">
							<el-input v-model="ruleForm.longitude" :placeholder="concatI18nT('message.common.请输入','message.iotdevice.设备经度')" maxlength="11" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.设备纬度')" prop="latitude">
							<el-input v-model="ruleForm.latitude" :placeholder="concatI18nT('message.common.请输入','message.iotdevice.设备纬度')" maxlength="11" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.激活时间')" prop="activeTime">
							<el-date-picker v-model="ruleForm.activeTime" type="date" placeholder="激活时间" />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.iotdevice.备注')" prop="remark">
							<el-input v-model="ruleForm.remark" :placeholder="concatI18nT('message.common.请输入','message.iotdevice.备注')" maxlength="500" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">{{$t('message.common.取消')}}</el-button>
					<el-button type="primary" @click="submit">{{$t('message.common.确定')}}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
	import { ref,onMounted } from "vue";
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	 import {i18n} from '/@/i18n/index'
   const { t } = i18n.global
	import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
  	import { formatDate } from '/@/utils/formatTime';
	import { addIotdevice, updateIotdevice, detailIotdevice } from "/@/api/iotdevice/iotdevice";
	import { pageIotProduct } from '/@/api/iotproduct/iotProduct';

	//父级传递来的参数
	var props = defineProps({
		title: {
		type: String,
		default: "",
	},
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
	const isShowDialog = ref(false);
	const ruleForm = ref<any>({});
		const productsData = ref<any>([]);
	//自行添加其他规则
	const rules = ref<FormRules>({
		deviceName: [{required: true, message: '请输入设备名称！', trigger: 'blur',},],
		productId: [{required: true, message: '请选择产品ID！', trigger: 'change',},],
		productName: [{required: true, message: '请输入产品名称！', trigger: 'blur',},],
		serialNumber: [{required: true, message: '请输入设备编号！', trigger: 'blur',},],
		firmwareVersion: [{required: true, message: '请输入固件版本！', trigger: 'blur',},],
		status: [{required: true, message: '请选择设备状态！', trigger: 'change',},],
	});

	// 页面加载时
	onMounted(async() => {
		var res = await pageIotProduct({});
		productsData.value = res.data.result?.items ?? [];
	});
	    const concatI18nT=(tips:string,comment:string)=>{
  return t(tips)+t(comment)
};

const changeProduct = (categoryName: string) => {//这里的sitename就是标签名
	ruleForm.value.productName = categoryName;
};
	// 打开弹窗
	const openDialog = async (row: any) => {
		// ruleForm.value = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		let rowData = JSON.parse(JSON.stringify(row));
		if (rowData.id)
			ruleForm.value = (await detailIotdevice(rowData.id)).data.result;
		else
			ruleForm.value = rowData;
		isShowDialog.value = true;
	};

	// 关闭弹窗
	const closeDialog = () => {
		emit("reloadTable");
		isShowDialog.value = false;
	};

	// 取消
	const cancel = () => {
		isShowDialog.value = false;
	};

	// 提交
	const submit = async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if (isValid) {
				let values = ruleForm.value;
				if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == "" || ruleForm.value.id == 0) {
					await addIotdevice(values);
				} else {
					await updateIotdevice(values);
				}
				closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};






	//将属性或者函数暴露给父组件
	defineExpose({ openDialog });
</script>




